package com.gshocklab.bulkposter.view.adapter;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gshocklab.bulkposter.R;
import com.gshocklab.bulkposter.data.dto.PostedMessage;

/**
 * Posted messages list adapter
 * @author vitaly gashock
 */

public final class PostedMessagesListAdapter extends ArrayAdapter<PostedMessage> {
	private List<PostedMessage> posts;
	private LayoutInflater inflater;
	
	public PostedMessagesListAdapter(Activity context, List<PostedMessage> posts) {
		super(context, R.layout.post_item, posts);
		
		this.posts = posts;
		this.inflater = context.getLayoutInflater();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemViewHolder itemViewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.post_item, parent, false);
			
			itemViewHolder = new ItemViewHolder();
			itemViewHolder.imageIcon = (ImageView) convertView.findViewById(R.id.icon_item);
			itemViewHolder.tvPost = (TextView) convertView.findViewById(R.id.post_text_item);
			itemViewHolder.tvPostDate = (TextView) convertView.findViewById(R.id.post_date_text_item);
			
			convertView.setTag(itemViewHolder);
		} else {
			itemViewHolder = (ItemViewHolder) convertView.getTag();
		}
		
		final PostedMessage post = posts.get(position);
		
		itemViewHolder.imageIcon.setImageResource(post.account.type.bigIconId);
		itemViewHolder.tvPost.setText(post.postText);
		itemViewHolder.tvPostDate.setText(post.postingDate.toLocaleString());
		itemViewHolder.tvPostDate.setTextColor(Color.DKGRAY);
		
		return convertView;
	}
	
	private class ItemViewHolder {
		public ImageView imageIcon;
		public TextView tvPost;
		public TextView tvPostDate;
	}
}
